package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.kjy.domain.ServiceAgency;
import com.ruoyi.kjy.domain.TalentPerson;
import com.ruoyi.kjy.service.ServiceAgencyService;
import com.ruoyi.kjy.service.TalentPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 10/25,0025 21:36
 */

@RestController
@RequestMapping("/serviceAgency")
public class ServiceAgencyController extends BaseController {


    @Autowired
    private ServiceAgencyService serviceAgencyService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, ServiceAgency serviceAgency) {
        QueryWrapper<ServiceAgency> queryWrapper = new QueryWrapper<>();
//        queryWrapper.setEntity(serviceAgency);
//        System.out.println(serviceAgency);
        if (serviceAgency.getServiceCategory() != null) {
            for (String category : serviceAgency.getServiceCategory()) {
                queryWrapper.like("service_category", category);
            }

        }
        if (serviceAgency.getName() != null) {
            queryWrapper.like("name", serviceAgency.getName());
        }
        if (serviceAgency.getContact() != null) {
            queryWrapper.like("contact", serviceAgency.getContact());
        }
        if (serviceAgency.getProvince() != null){
            queryWrapper.like("province", serviceAgency.getProvince());
        }
        if (serviceAgency.getCity() != null){
            queryWrapper.like("city", serviceAgency.getCity());
        }
        if (Objects.isNull(current)) {
            current = 1L;
        }
        if (Objects.isNull(size)) {
            size = 10L;
        }
        if (size >= 20L) {
            size = 20L;
        }
        return AjaxResult.success(serviceAgencyService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(serviceAgencyService.getById(id));
    }


    @Log(title = "服务机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceAgency serviceAgency) {
        serviceAgency.setStatus(0);
        boolean save = serviceAgencyService.save(serviceAgency);
        return toAjax(save);
    }

    @Log(title = "服务机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceAgency serviceAgency) {
        return toAjax(serviceAgencyService.updateById(serviceAgency));
    }

    @Log(title = "服务机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(serviceAgencyService.removeByIds(Arrays.asList(ids)));
    }


}
